import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import math
import random
import os
import time
from PSO import PSO
from SSA import SSA
from SSA2020 import SSA2020
from SCA import SCA
from AOA import AOA

from SAOA import SAOA
from MSAOA import MSAOA
from AOA1 import AOA1
from AOA2 import AOA2
from AOA3 import AOA3
from WOA import WOA
from GSA import GSA
from TLBO import TLBO
import obj_funs
from aoatest import AOAS

detail = {
    'F1':[-100,100,2],
    'F2':[-10,10,0.2],
    'F3':[-100,100,2],
    'F4':[-100,100,2],
    'F5':[-30,30,0.5],
    'F6':[-100,100,2],
    'F7':[-1.28,1.28,0.02],
    'F8':[-500,500,10],   #多峰开始
    'F9':[-5.12,5.12,0.2],
    'F10':[-30,30,0.5],
    'F11':[-600,600,5],
    'F12':[-50,50,1],
    'F13':[-50,50,1],
    'F14':[-65,65,2],  #2维
    'F15':[-5,5,0.2],  #4维
    'F16':[-5,5,0.2],  #2维
    'F17':[-5,5,0.2],  #2维
    'F18':[-2,2,0.1],  #2维
    'F19':[1,3,0.1],   #3维
    'F20':[0,1,0.05],  #6维
    'F21':[0,10,0.5],  #4维
    'F22':[0,10,0.5],  #4维
    'F23':[0,10,0.5]   #4维
}

#画图
def chart(fit_data,avg_fit_data,avg_ts_data):
    plt.figure(num=1,figsize=(9,7))
    plt.subplot(2,2,1)
    plt.xlabel('迭代次数')
    plt.ylabel('适应值')
    plt.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
    plt.grid(True, linestyle='--', alpha=0.5)
    for i in range(len(fit_data)):
        plt.semilogy(fit_data[i][1],label=fit_data[i][0])
    plt.rcParams['font.sans-serif']=['Microsoft YaHei']
    plt.rcParams['axes.unicode_minus']=False
    plt.legend()
    
    plt.subplot(2,2,2)
    plt.xlabel('迭代次数')
    plt.ylabel('适应均值')
    plt.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
    plt.grid(True, linestyle='--', alpha=0.5)
    for i in range(len(avg_fit_data)):
        plt.semilogy(avg_fit_data[i][1],label=avg_fit_data[i][0])
    plt.rcParams['font.sans-serif']=['Microsoft YaHei']
    plt.rcParams['axes.unicode_minus']=False
    plt.legend()

    #semilogy
    plt.subplot(2,2,3)
    plt.xlabel('迭代次数')
    plt.ylabel('平均方差')
    plt.ticklabel_format(style='sci', scilimits=(0,0), axis='y')
    plt.grid(True, linestyle='--', alpha=0.5)
    for i in range(len(avg_ts_data)):
        plt.semilogy(avg_ts_data[i][1],label=avg_ts_data[i][0])
    plt.rcParams['font.sans-serif']=['Microsoft YaHei']
    plt.rcParams['axes.unicode_minus']=False
    plt.legend()
    plt.show()

#迭代参数
dimensions = 30
populationSize = 10
iterations = 500
fun='F9'
low = [detail[fun][0] for i in range(dimensions)]
up = [detail[fun][1] for i in range(dimensions)]
obj_func = getattr(obj_funs, fun)



# pso = PSO(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# pso_best_fit,pso_avg_fit,pso_avg_ts =  pso.pso()
# ssa = SSA(dimensions,populationSize,iterations ,low, up,obj_func=obj_func)
# ssa_best_fit,ssa_avg_fit,ssa_avg_ts = ssa.ssa()
# ssa2020 = SSA2020(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# ssa2020_best_fit,ssa2020_avg_fit,ssa2020_avg_ts = ssa2020.Tent_SSA()
# sca = SCA(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# sca_best_fit,sca_avg_fit,sca_avg_ts = sca.sca()
aoa = AOA(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
aoa__best_fit,aoa_avg_fit,aoa_avg_ts = aoa.aoa()
# aoas = AOAS(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# aoas__best_fit,aoas_avg_fit,aoas_avg_ts = aoas.aoa()
saoa = SAOA(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
saoa__best_fit,saoa_avg_fit,saoa_avg_ts = saoa.aoa()

#以下是部分优化的阿基米德算法
# msaoa = MSAOA(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# msaoa__best_fit,msaoa_avg_fit,msaoa_avg_ts = msaoa.msaoa()
# aoa1 = AOA1(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# aoa1__best_fit,aoa1_avg_fit,aoa1_avg_ts = aoa1.aoa()
# aoa2 = AOA2(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# aoa2__best_fit,aoa2_avg_fit,aoa2_avg_ts = aoa2.aoa()
# aoa3 = AOA3(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# aoa3__best_fit,aoa3_avg_fit,aoa3_avg_ts = aoa3.aoa()

# woa = WOA(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# woa__best_fit,woa_avg_fit,woa_avg_ts = woa.Tent_WOA()
# gsa = GSA(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# gsa__best_fit,gsa_avg_fit,gsa_avg_ts = gsa.Tent_GSA()
# tlbo = TLBO(dimensions, populationSize, iterations, low, up, obj_func=obj_func)
# tlbo__best_fit,tlbo_avg_fit,tlbo_avg_ts = tlbo.Tent_TLBO()
fit_data=[]
avg_fit_data = []
avg_ts_data = []
# fit_data.append(['PSO',pso_best_fit])
# fit_data.append(['SSA',ssa_best_fit])
# fit_data.append(['SSA2020',ssa2020_best_fit])
# fit_data.append(['SCA',sca_best_fit])
fit_data.append(['AOA',aoa__best_fit])
# fit_data.append(['AOAs',aoas__best_fit])
fit_data.append(['SAOA',saoa__best_fit])
# fit_data.append(['MSAOA',msaoa__best_fit])
# fit_data.append(['AOA1',aoa1__best_fit])
# fit_data.append(['AOA2',aoa2__best_fit])
# fit_data.append(['AOA3',aoa3__best_fit])
# fit_data.append(['WOA',woa__best_fit])
# fit_data.append(['GSA',gsa__best_fit])
# fit_data.append(['TLBO',tlbo__best_fit])



# avg_fit_data.append(['PSO',pso_avg_fit])
# avg_fit_data.append(['SSA',ssa_avg_fit])
# avg_fit_data.append(['SSA2020',ssa2020_avg_fit])
# avg_fit_data.append(['SCA',sca_avg_fit])
avg_fit_data.append(['AOA',aoa_avg_fit])
# avg_fit_data.append(['AOAs',aoas_avg_fit])
avg_fit_data.append(['SAOA',saoa_avg_fit])
# avg_fit_data.append(['MSAOA',msaoa_avg_fit])
# avg_fit_data.append(['AOA1',aoa1_avg_fit])
# avg_fit_data.append(['AOA2',aoa2_avg_fit])
# avg_fit_data.append(['AOA3',aoa3_avg_fit])
# avg_fit_data.append(['WOA',woa_avg_fit])
# avg_fit_data.append(['GSA',gsa_avg_fit])
# avg_fit_data.append(['TLBO',tlbo_avg_fit])


# avg_ts_data.append(['PSO',pso_avg_ts])
# avg_ts_data.append(['SSA',ssa_avg_ts])
# avg_ts_data.append(['SSA2020',ssa2020_avg_ts])
# avg_ts_data.append(['SCA',sca_avg_ts])
avg_ts_data.append(['AOA',aoa_avg_ts])
# avg_ts_data.append(['AOAs',aoas_avg_ts])
avg_ts_data.append(['SAOA',saoa_avg_ts])
# avg_ts_data.append(['MSAOA',msaoa_avg_ts])
# avg_ts_data.append(['AOA1',aoa1_avg_ts])
# avg_ts_data.append(['AOA2',aoa2_avg_ts])
# avg_ts_data.append(['AOA3',aoa3_avg_ts])
# avg_ts_data.append(['WOA',woa_avg_ts])
# avg_ts_data.append(['GSA',gsa_avg_ts])
# avg_ts_data.append(['TLBO',tlbo_avg_ts])



chart(fit_data,avg_fit_data,avg_ts_data)

